package com.yun.zf.system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yun.zf.common.admin.domain.SysRole;
import com.yun.zf.common.admin.domain.SysUser;
import com.yun.zf.common.admin.dto.sys.RoleListDto;
import com.yun.zf.common.admin.model.TreeMenuMo;
import com.yun.zf.common.admin.model.sys.RoleAddMo;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 角色表 数据层
 * 
 * @author ruoyi
 */
@Mapper
public interface SysRoleMapper extends BaseMapper<SysRole>
{


    /**
     * 根据用户ID查询角色
     * 
     * @param userId 用户ID
     * @return 角色列表
     */

    @Select("<script> " +
            " SELECT DISTINCT r.* from sys_user u ,sys_user_role ur , sys_role r where  ur.role_id= r.role_id and  u.user_id=ur.user_id  and r.del_flag='0'  and  u.user_id=#{userId} " +
            "</script>")
    public List<SysRole> selectRolePermissionByUserId(Long userId);





    /**
     * 修改角色信息
     *
     * @param role 角色信息
     * @return 结果
     */

    @Insert(
            " INSERT INTO sys_role ( role_name, role_key, role_sort, status, create_time, update_time, remark ) " +
                    "VALUES ( #{role.roleName}, #{role.roleKey}, #{role.roleSort}, #{role.status}, #{role.createTime}, #{role.updateTime}, #{role.remark} ) "
            )
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="role.roleId", before=false, resultType=Long.class)
    public int insertRole(@Param("role") RoleAddMo role);

    /**
     * 修改角色信息
     * 
     * @param role 角色信息
     * @return 结果
     */
    @Update("<script>" +
            " update sys_role\n" +
            "  <set> " +
            " <if test=\"roleName != null and roleName != ''\">role_name = #{roleName},</if> " +
            " <if test=\"roleKey != null and roleKey != ''\">role_key = #{roleKey},</if> " +
            " <if test=\"roleSort != null and roleSort != ''\">role_sort = #{roleSort},</if> " +
            " <if test=\"status != null and status != ''\">status = #{status},</if> " +
            " <if test=\"remark != null\">remark = #{remark},</if> " +
            " update_time = sysdate() " +
            " </set> " +
            " where role_id = #{roleId}" +
            "</script>")
    public int updateRole(RoleAddMo role);

    @Update("<script>" +
            " update sys_role\n" +
            "  <set> " +
            " <if test=\"status != null and status != ''\">status = #{status},</if> " +
            " update_time = sysdate() " +
            " </set> " +
            " where role_id = #{roleId}" +
            "</script>")
    public int updateRoleStatus(Long roleId,String status);

    @Update("<script>" +
            " update sys_role\n" +
            "  <set> " +
            " <if test=\"dataScope != null and dataScope != ''\">data_scope = #{dataScope},</if> " +
            " update_time = sysdate() " +
            " </set> " +
            " where role_id = #{roleId}" +
            "</script>")
    public int updateRoleScope(Long roleId,String dataScope);


    /**
     * 通过角色ID删除角色
     * 
     * @param roleId 角色ID
     * @return 结果
     */
    public int deleteRoleById(Long roleId);

    /**
     * 批量删除角色信息
     * 
     * @param roleIds 需要删除的角色ID
     * @return 结果
     */
    @Select("<script>" +
            "   update sys_role set del_flag = '2' where role_id in  " +
            " <foreach collection=\"roleIds\" item=\"roleId\" open=\"(\" separator=\",\" close=\")\">\n" +
            " #{roleId}" +
            " </foreach>  " +
            "</script>")
    public void deleteRoleByIds(Long[] roleIds);


    @Select("SELECT r.* from sys_user_role ur,sys_role r where ur.role_id=r.role_id and ur.user_id= #{userId}")
    public List<SysRole> getRoles(Long userId);

    @Select("<script>" +
            " SELECT DISTINCT  m.menu_id  ,m.menu_name   ,m.parent_id,m.perms  from sys_role_menu rm ,sys_menu m WHERE rm.menu_id=m.menu_id and rm.role_id=#{roleId} " +
            "</script>")
    public List<TreeMenuMo> selectMenuById(Long roleId);

    @Select(" SELECT role_id ,role_name from sys_role WHERE `status`=0 ")
    List<RoleListDto> getInAgentRole();
}
